package com.plian.system.controller.im;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.plian.system.dto.im.FundProjectExecutionDTO;
import com.plian.system.dto.im.FundProjectInfoDTO;
import com.plian.system.entity.im.FundProjectExecution;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.im.IFundProjectExecutionService;
import com.plian.system.service.im.IFundProjectService;
import com.plian.system.vo.im.FundProjectExecutionVO;
import com.plian.system.vo.im.FundProjectVO;
import com.plian.system.wrapper.im.FundProjectExecutionWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.List;

/**
 * @author panqb
 * @version 1.0
 * @description:市属国企基金项目 控制器
 * @date 2022/4/29 下午6:27
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/fundproject")
@Api(value = "FundProjectController", tags = "市属国企基金项目 控制器")
public class FundProjectController {

    private final IFundProjectExecutionService fundProjectExecutionService;

    private FundProjectExecutionWrapper fundProjectExecutionWrapper;

    private IFundProjectService fundProjectService;
    /**
     * 市属国企基金项目统计表
     * @param query
     * @return
     */
    @PostMapping("statisticsReport")
    @ApiOperation(value = "统计报表", notes = "市属国企基金项目统计表")
    public ListResult<FundProjectVO> statisticsReport(@RequestBody Query query){
        ListResult<FundProjectVO> listResult=new ListResult<>(CommonCode.SUCCESS);
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs()==null&&query.getDescs()==null){
            query.setDescs("filing_month");
        }
        FundProjectInfoDTO fundProjectInfoDTO=new FundProjectInfoDTO();
        Iterator<SearchEntity> iterable=entityList.iterator();
        while (iterable.hasNext()){
            SearchEntity entity=iterable.next();
            String key=entity.getField();
            if("fund_nature".equals(key)){
                List<String> fundNatures= (List<String>) entity.getValue();
                fundProjectInfoDTO.setFundNatures(fundNatures);
                iterable.remove();
            }
        }
        QueryWrapper<FundProjectExecution> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new FundProjectExecutionDTO()), entityList
        );
        TokenSearch<FundProjectExecution> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);
        List<FundProjectExecution> executionList = fundProjectExecutionService.list(queryWrapper);
        List<FundProjectExecutionVO> fundProjectExecutionVOS = fundProjectExecutionWrapper.entityToVO(executionList);
        List<FundProjectVO> list=fundProjectExecutionService.executionListDetail(fundProjectExecutionVOS,fundProjectInfoDTO);
        listResult.setList(list);
        listResult.setTotal(list.size());
        return listResult;
    }

    /**
     * 导出市属国企基金项目统计表
     * @param query
     * @return
     */
    @PostMapping("exportReport")
    @ApiOperation(value = "导出市属国企基金项目统计表")
    public ObjResult exportReport(@RequestBody Query query, HttpServletResponse response){
        ListResult<FundProjectVO> listResult=new ListResult<>(CommonCode.SUCCESS);
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs()==null&&query.getDescs()==null){
            query.setDescs("filing_month");
        }
        FundProjectInfoDTO fundProjectInfoDTO=new FundProjectInfoDTO();
        Iterator<SearchEntity> iterable=entityList.iterator();
        while (iterable.hasNext()){
            SearchEntity entity=iterable.next();
            String key=entity.getField();
            if("fund_nature".equals(key)){
                List<String> fundNatures= (List<String>) entity.getValue();
                fundProjectInfoDTO.setFundNatures(fundNatures);
                iterable.remove();
            }
        }
        QueryWrapper<FundProjectExecution> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new FundProjectExecution()), entityList
        );
        TokenSearch<FundProjectExecution> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);
        List<FundProjectExecution> executionList = fundProjectExecutionService.list(queryWrapper);
        List<FundProjectExecutionVO> fundProjectExecutionVOS = fundProjectExecutionWrapper.entityToVO(executionList);
        fundProjectService.exportReport(fundProjectExecutionVOS,fundProjectInfoDTO,response);
        ObjResult objResult = new ObjResult(CommonCode.SUCCESS);
        return objResult;
    }
}
